package org.cnam.mobileShopping.wsclient;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.cnam.mobileShopping.bean.Category;
import org.cnam.mobileShopping.bean.MList;
import org.cnam.mobileShopping.bean.Product;
import org.cnam.mobileShopping.bean.Productlist;
import org.cnam.mobileShopping.bean.Store;
import org.cnam.mobileShopping.bean.User;
import org.cnam.mobileShopping.util.MobileShoppingConstants;
import org.cnam.mobileShopping.util.PatternUtil;
import org.json.JSONException;

public class WSClient extends MobileShoppingConstants {
	private static final WSClient INSTANCE = new WSClient();

	private static final String TAG = "WSClient";

	private static final String BASE_URL = "http://" + BASE_HOST
			+ "/mobileshopping-ws/resources/";

	public static WSClient getInstance() {
		return INSTANCE;
	}

	private WSClient() {
	}

	// PRODUCT
	public List<Category> getCategoryList() throws IOException, JSONException {
		return WSHelper.getEntityList(BASE_URL + "product/getCategoriesList",
				Category.class);
	}

	public List<Category> getCategoryList(Date lastupdate) throws IOException,
			JSONException {
		// TODO appeler la méthode qui récupère que les nouvelles catégories
		return WSHelper.getEntityList(BASE_URL + "product/getCategoriesList",
				Category.class);
	}

	public List<Product> getProductsByCategory(Integer categoryId)
			throws IOException, JSONException {
		return WSHelper.getEntityList(BASE_URL
				+ "product/getProductsByCategory?category=" + categoryId,
				Product.class);
	}

	public List<Product> getProductsForSynchro(Date lastupdate)
			throws IOException, JSONException {
		// création de la chaine date au format dd/MM/yyyy
		String d = PatternUtil.formatDate(lastupdate);
		return WSHelper.getEntityList(BASE_URL
				+ "product/getProductsForSynchro?lastupdatedatetime=" + d,
				Product.class);
	}

	public Product addProductInReferenciel(Product product) throws IOException,
			JSONException {
		return WSHelper.getEntity(
				BASE_URL + "product/addProductInReferenciel?description="
						+ URLEncoder.encode(product.getDescription(), "UTF-8")
						+ "&barcode=" + product.getBarcode() + "&categoryid="
						+ product.getCategoryid(), Product.class);
	}

	// USER
	public User getUser(String login, String password) throws IOException,
			JSONException {
		return WSHelper.getEntity(BASE_URL + "user/auth?login=" + login
				+ "&password=" + password, User.class);
	}

	public List<User> getUsers() throws IOException, JSONException {
		return WSHelper.getEntityList(BASE_URL + "user/getUsers", User.class);
	}

	// LIST
	public MList addList(MList mlist, Integer userId) throws IOException,
			JSONException {
		return WSHelper.getEntity(BASE_URL + "mobileList/addList?libelle="
				+ URLEncoder.encode(mlist.getLabel(), "UTF-8") + "&userId="
				+ userId + "&commonid=" + mlist.getCommonid() + "&statusid="
				+ mlist.getStatus() + "&version=" + mlist.getVersion(),
				MList.class);
	}

	public MList getList(String commonId) throws IOException, JSONException {
		return WSHelper.getEntity(BASE_URL + "mobileList/getList?commonlistid="
				+ URLEncoder.encode(commonId, "UTF-8"), MList.class);
	}

	public List<Productlist> getProductList(String commonlistId)
			throws IOException, JSONException {
		return WSHelper.getEntityList(BASE_URL
				+ "mobileList/getProductsListByComonId?commonlistid="
				+ URLEncoder.encode(commonlistId, "UTF-8"), Productlist.class);
	}

	public MList updateList(MList mlist, Integer userId) throws IOException,
			JSONException {
		return WSHelper.getEntity(
				BASE_URL + "mobileList/updateList?commonlistId="
						+ URLEncoder.encode(mlist.getCommonid(), "UTF-8")
						+ "&libelle="
						+ URLEncoder.encode(mlist.getLabel(), "UTF-8")
						+ "&statusid=" + mlist.getStatus() + "&version="
						+ mlist.getVersion(), MList.class);
	}

	public void deleteList(MList mlist, Integer userId) throws IOException,
			JSONException {
		WSHelper.call(BASE_URL + "mobileList/deleteList?commonlistId="
				+ URLEncoder.encode(mlist.getCommonid(), "UTF-8") + "&userid="
				+ userId);
	}

	public List<MList> getListsForUser(Integer userId) throws IOException,
			JSONException {
		return WSHelper.getEntityList(BASE_URL
				+ "mobileList/getListsForUser?userId=" + userId, MList.class);
	}

	public void sharedList(Integer userId, MList mlist) throws IOException,
			JSONException {
		WSHelper.call(BASE_URL + "mobileList/sharedList?commonlistid="
				+ URLEncoder.encode(mlist.getCommonid(), "UTF-8") + "&userId="
				+ userId);
	}

	// PRODUCTLIST
	public Productlist addProductInList(String commonlistId,
			Productlist productlist, Integer userId) throws IOException,
			JSONException {
		return WSHelper.getEntity(
				BASE_URL + "mobileList/addProduct?commonlistId="
						+ URLEncoder.encode(commonlistId, "UTF-8") + "&userId="
						+ userId + "&produitId=" + productlist.getProductid()
						+ "&quantity=" + productlist.getQuantity()
						+ "&commonid="
						+ URLEncoder.encode(productlist.getCommonid(), "UTF-8")
						+ "&version=" + productlist.getVersion(),
				Productlist.class);
	}

	public void deleteProductInList(String commonproductListId, Integer userId)
			throws IOException, JSONException {
		WSHelper.call(BASE_URL
				+ "mobileList/deleteProduct?commonproductListId="
				+ URLEncoder.encode(commonproductListId, "UTF-8") + "&userId="
				+ userId);
	}

	public Productlist updateProductInList(Productlist productlist,
			Integer userId) throws IOException, JSONException {
		return WSHelper.getEntity(
				BASE_URL + "mobileList/updateProduct?commonproductListId="
						+ productlist.getCommonid() + "&userId=" + userId
						+ "&quantity=" + productlist.getQuantity()
						+ "&version=" + productlist.getVersion() + "&status="
						+ productlist.getStatusid(), Productlist.class);
	}

	public List<Store> getStores(String commonlistId) {
		List<Store> stores = new ArrayList<Store>();

		stores.add(new Store(1, "Casino La cépière", 1.415151, 43.58997));
		stores.add(new Store(2, "Auchan Balma", 1.490936, 43.662507));
		stores.add(new Store(3, "E.LECLERC Blagnac", 1.370487, 43.653044));
		return stores;
	}

}
